package eval.host.compare;

import java.util.Collections;
import java.util.List;
import com.google.inject.Singleton;
import eval.host.Host;
import eval.utilities.configuration.ExperimentConfiguration;

/**
 * This class is used to sort the hosts in the cloud,
 * according to the delta between the current CPU load and the maximum line in each host.
 * 
 * @author Amit Peled   <amit.kapina@gmail.com>
 * @author Igal Kaufman <igal.kaufman@gmail.com> 
 */
@Singleton
public class MaxLineHostSortList implements ISortHostList{
	protected ExperimentConfiguration conf = ExperimentConfiguration.getSingleton();

	/* (non-Javadoc)
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	@Override
	public int compare(Host host1, Host host2) {
		double hostLoad = host1.getCurrentCpuLoad();
		double hostCapcity = host1.getHostCapacity().getCpuCapacity();
		double maxLine = (conf.MaxLine/100.0)*hostCapcity;
		double deltaHost1 = maxLine - hostLoad;

		hostLoad = host2.getCurrentCpuLoad();
		hostCapcity = host2.getHostCapacity().getCpuCapacity();
		maxLine = (conf.MaxLine/100.0)*hostCapcity;
		double deltaHost2 = maxLine - hostLoad;

		return (int)(deltaHost1-deltaHost2);


	}

	/* (non-Javadoc)
	 * @see eval.host.compare.ISortHostList#sortHostList(java.util.List)
	 */
	@Override
	public List<Host> sortHostList(List<Host> hostToSort) {
		Collections.sort(hostToSort,this);
		return hostToSort;
	}

}
